package cn.com.yang;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class GroupByListMapDemo {
    public static void main(String[] args) {
        // 创建List<Map<String, Object>>对象
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map1 = new HashMap<>();
        map1.put("name", "Alice");
        map1.put("age", 18);
        map1.put("category", "A");
        list.add(map1);

        Map<String, Object> map2 = new HashMap<>();
        map2.put("name", "Bob");
        map2.put("age", 20);
        map2.put("category", "A");
        list.add(map2);

        Map<String, Object> map3 = new HashMap<>();
        map3.put("name", "Charlie");
        map3.put("age", 22);
        map3.put("category", "B");
        list.add(map3);

        // 根据category属性进行分组
        Map<String, List<Map<String, Object>>> groupedMap = list.stream()
                .collect(Collectors.groupingBy(map -> map.get("category").toString()));

        System.out.println("1111:"+groupedMap.toString());
        // 打印分组结果
        groupedMap.forEach((category, maps) -> {
            System.out.println("Category: " + category);
            maps.forEach(map -> System.out.println("Name: " + map.get("name") + ", Age: " + map.get("age")));
            System.out.println("------------------------");
        });
    }
}